package com.sev.Dao.DAOimpl;

import com.sev.Dao.UserDao;
import org.springframework.stereotype.Repository;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;

@Repository("userDAO")
public class UserDAOlmpl implements UserDao {
    private PreparedStatement pStatement = null;
    private ResultSet rs = null;
    private Connection ct = null;

    public void UserDAOImpl() {

    }

    @Override
    public ArrayList getUsers() {
        ArrayList resultsList = new ArrayList();

        try {
            this.ct = (new ConnDBMysql()).getConn();
            String querySQL = "select * from users";
            this.pStatement = this.ct.prepareStatement(querySQL);
            this.rs = this.pStatement.executeQuery();

            while(this.rs.next()) {
                UserBean ub = new UserBean();
                ub.setUserId(this.rs.getLong(1));
                ub.setUsername(this.rs.getString(2));
                ub.setPasswd(this.rs.getString(3));
                resultsList.add(ub);
            }
        } catch (Exception var7) {
            var7.printStackTrace();
        } finally {
            this.close();
        }

        return resultsList;
    }

    @Override
    public boolean login(String username, String password) {
        boolean flag = false;
        if (username != null && password != null) {
            try {
                this.ct = (new ConnDBMysql()).getConn();
                String selectSQL = "select password from users where username=?";
                this.pStatement = this.ct.prepareStatement(selectSQL);
                this.pStatement.setString(1, username);
                this.rs = this.pStatement.executeQuery();
                if (this.rs.next() && this.rs.getString(1).equals(password)) {
                    flag = true;
                }
            } catch (Exception var8) {
                var8.printStackTrace();
            } finally {
                this.close();
            }
        }

        return flag;
    }

    @Override
    public boolean addUser(UserBean userbean) {
        boolean flag = false;

        try {
            this.ct = (new ConnDBMysql()).getConn();
            String insertSQL = "insert into users (username, password) values (?, ?)";
            this.pStatement = this.ct.prepareStatement(insertSQL);
            this.pStatement.setString(1, userbean.getUsername());
            this.pStatement.setString(2, userbean.getPasswd());
            int count = this.pStatement.executeUpdate();
            if (count > 0) {
                flag = true;
            }
        } catch (Exception var8) {
            var8.printStackTrace();
        } finally {
            this.close();
        }

        return flag;
    }

    @Override
    public boolean updateUser(UserBean userBean) {
        boolean flag = false;

        try {
            this.ct = (new ConnDBMysql()).getConn();
            String updateSQL = "update users SET username= ?, password=? where userId= ?";
            this.pStatement = this.ct.prepareStatement(updateSQL);
            this.pStatement.setString(1, userBean.getUsername());
            this.pStatement.setString(2, userBean.getPasswd());
            this.pStatement.setLong(3, userBean.getUserId());
            int count = this.pStatement.executeUpdate();
            if (count > 0) {
                flag = true;
            }
        } catch (Exception var8) {
            var8.printStackTrace();
        } finally {
            this.close();
        }

        return flag;
    }

    @Override
    public boolean delUserById(Long id) {
        boolean flag = false;

        try {
            this.ct = (new ConnDBMysql()).getConn();
            String deleteSQL = "delete from users where userId= ?";
            this.pStatement = this.ct.prepareStatement(deleteSQL);
            this.pStatement.setLong(1, id);
            int count = this.pStatement.executeUpdate();
            if (count > 0) {
                flag = true;
            }
        } catch (Exception var5) {
            var5.printStackTrace();
        }

        return flag;
    }

    public void close() {
        try {
            if (this.rs != null) {
                this.rs.close();
                this.rs = null;
            }

            if (this.pStatement != null) {
                this.pStatement.close();
                this.pStatement = null;
            }

            if (this.ct != null) {
                this.ct.close();
                this.ct = null;
            }
        } catch (Exception var2) {
            var2.printStackTrace();
        }

    }
}
